<?php

/**
 * prestamo actions.
 *
 * @package    cronos-doctrine
 * @subpackage prestamo
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class prestamoActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->prestamos = Doctrine_Core::getTable('Prestamo')
      ->createQuery('a')
      ->execute();
    
    
   /* 
    if(strlen($this->prestamos)<=0){
        
        $this->redirect('prestamo/new');
        exit;
    }*/
    $camioneta = new Camioneta();
    $camioneta = $camioneta->getCamionetaGuardUser($this->getUser()->getGuardUser()->getId());
  
    if(!isset($camioneta)){
        $this->camioneta_id = $camioneta->getId();
    }else {
        $this->camioneta_id = 1;
    } 
            
    //$this->camioneta_id = 2;
    
    $camioneta = new Camioneta();
    $this->camioneta =  $camioneta->getCamionetaId($this->camioneta_id);
    
    $this->fechaRuta = date('Y')."-".date('m')."-".date('d');
   
    $agendado = new Agendado();
    $this->agendados = $agendado->getAgendadoRuta($this->camioneta_id, $this->fechaRuta);
    
    
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PrestamoForm();
    
    
    $this->form->setWidget('usuario_id', new sfWidgetFormInputHidden(array(),array('value' => $this->getUser()->getGuardUser()->getId())));
    $this->form->setWidget('prestamo_status_id', new sfWidgetFormInputHidden(array(),array('value' => 1)));
    
    $camioneta = new Camioneta();
    $camioneta = $camioneta->getCamionetaGuardUser($this->getUser()->getGuardUser()->getId());
  
    if(!isset($camioneta)){
        $this->camioneta_id = $camioneta->getId();
    }else {
        $this->camioneta_id = 1;
    } 
    //$this->camioneta_id = 2;
    
    $camioneta = new Camioneta();
    $this->camioneta =  $camioneta->getCamionetaId($this->camioneta_id);
    
    $this->fechaRuta = date('Y')."-".date('m')."-".date('d');
   
    $agendado = new Agendado();
    $this->agendados = $agendado->getAgendadoRuta($this->camioneta_id, $this->fechaRuta);
    
    
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new PrestamoForm();
    $this->form->setWidget('usuario_id', new sfWidgetFormInputHidden(array(),array('value' => $this->getUser()->getGuardUser()->getId())));
    $this->form->setWidget('prestamo_status_id', new sfWidgetFormInputHidden(array(),array('value' => 1)));
    $this->processForm($request, $this->form);
    $camioneta = new Camioneta();
    $camioneta = $camioneta->getCamionetaGuardUser($this->getUser()->getGuardUser()->getId());
  
    if(!isset($camioneta)){
        $this->camioneta_id = $camioneta->getId();
    }else {
        $this->camioneta_id = 1;
    } 
    
    $camioneta = new Camioneta();
    $this->camioneta =  $camioneta->getCamionetaId($this->camioneta_id);
    
    $this->fechaRuta = date('Y')."-".date('m')."-".date('d');
   
    $agendado = new Agendado();
    $this->agendados = $agendado->getAgendadoRuta($this->camioneta_id, $this->fechaRuta);
    
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($prestamo = Doctrine_Core::getTable('Prestamo')->find(array($request->getParameter('id'))), sprintf('Object prestamo does not exist (%s).', $request->getParameter('id')));
    $this->form = new PrestamoForm($prestamo);
            
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($prestamo = Doctrine_Core::getTable('Prestamo')->find(array($request->getParameter('id'))), sprintf('Object prestamo does not exist (%s).', $request->getParameter('id')));
    $this->form = new PrestamoForm($prestamo);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($prestamo = Doctrine_Core::getTable('Prestamo')->find(array($request->getParameter('id'))), sprintf('Object prestamo does not exist (%s).', $request->getParameter('id')));
    $prestamo->delete();

    $this->redirect('prestamo/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $prestamo = $form->save();

      $this->redirect('prestamo/edit?id='.$prestamo->getId());
    }
  }
}
